package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.User;
import com.mysql.DBHelper;

/**
 * 后台用户操作
 * 
 */
@Component
public class UserDao {
    /**
     * 用户登录
     */
    public Map<String, Object> login(User user) {
        List<Map<String, Object>> list = DBHelper.executeQuery(
            "select emp.id,emp.realname,emp.icon,emp.phone,emp.password,emp.is_status,ro.id role_id from employee emp "
                + "left join role_emp re on emp.id=re.emp_id left join role ro on re.role_id=ro.id "
                + "left join department dep on ro.dep_id=dep.id where emp.is_deleted=0 and emp.is_status=0  and emp.phone=?"
                + "and emp.password=?",
            user.getPhone(), user.getPassword());
        return list.size() > 0 ? list.get(0) : null;
    }

    /**
     * 查询一个用户账户（手机号）是否存在
     */
    public Map<String, Object> findOneByPhone(User user) {
        List<Map<String, Object>> list = DBHelper.executeQuery(
            "select emp.id,emp.realname,emp.phone,emp.is_status,ro.id role_id from employee emp "
                + "left join role_emp re on emp.id=re.emp_id left join role ro on re.role_id=ro.id "
                + "left join department dep on ro.dep_id=dep.id where emp.is_deleted=0 and emp.phone=?",
            user.getPhone());
        return list.size() > 0 ? list.get(0) : null;
    }

    /**
     * 用户修改密码
     */
    public int updatePassword(User user) {
        return DBHelper.executeUpdate("update employee set password=? where phone=?", user.getPassword(),
            user.getPhone());
    }
}
